package filterbar;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import org.an.proj.contacts.R;

/**
 * 创建时间：2017年01月12日20:05 <br>
 * 作者：fuchaoyang <br>
 * 描述：筛选框筛选条
 */

public class FilterBarLayout extends LinearLayout {

  private OnClickTabListener mClickTabListener;

  /**
   * ColorStateList selected选中态，默认态
   */
  @ColorRes private int mTabTextColor = -1;

  private List<FilterTabEntity> mTabEntities;
  /**
   * 上次点击的第几个tab
   */
  private int mLastPos = -1;

  public FilterBarLayout(Context context) {
    super(context, null);
  }

  public FilterBarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(HORIZONTAL);
    // 如果要支持代码中设置文字颜色，和icon可以添加自定义属性
  }

  /**
   * 获取上次点击的tab index
   */
  public int getLastPos() {
    return mLastPos;
  }

  public FilterTabEntity getTabEntity(int index) {
    return mTabEntities.get(index);
  }

  public void setTabTextColor(int tabTextColor) {
    mTabTextColor = tabTextColor;
  }

  public void setClickTabListener(OnClickTabListener clickTabListener) {
    mClickTabListener = clickTabListener;
  }

  /**
   * @param list 不能empty
   */
  public void setData(@NonNull List<FilterTabEntity> list) {
    if (list == null || list.size() == 0) {
      throw new IllegalStateException("FilterTitleEntity can not be NULL or EMPTY !");
    }

    mTabEntities = list;
    notifyDataSetChanged();
  }

  /**
   * 目前不支持局部刷新，统一刷新即可
   */
  public void notifyDataSetChanged() {
    removeAllViews();

    View tabView;
    for (int i = 0; i < mTabEntities.size(); i++) {
      tabView = View.inflate(getContext(), R.layout.view_fitler_tab, null);
      //tabView.setTag(i);
      addTab(i, tabView);
    }
  }

  /**
   * 重置tab
   */
  public void resetTab(int index) {
    mTabEntities.get(index).reset();
    updateTab(index);
  }

  public void updateTab(FilterTabEntity tabEntity) {
    View tabView = getChildAt(tabEntity.position);
    TextView tvTabTitle = (TextView) tabView.findViewById(R.id.tv_tab_title);
    tvTabTitle.setSelected(tabEntity.isHighLight);
    // 如果要重置，调用entity.reset方法，调用下面的方法也是可以的
    //tvTabTitle.setText(tabEntity.showTitle);

    // 高亮则显示showTitle，否则显示默认title
    tvTabTitle.setText(tabEntity.isHighLight ? tabEntity.showTitle : tabEntity.defaultTitle);
  }

  /**
   * 更新tab,需要手动修改FilterTabEntity
   *
   * @param index index
   */
  public void updateTab(int index) {
    View tabView = getChildAt(index);
    TextView tvTabTitle = (TextView) tabView.findViewById(R.id.tv_tab_title);
    FilterTabEntity tabEntity = mTabEntities.get(index);
    tvTabTitle.setSelected(tabEntity.isHighLight);
    // 如果要重置，调用entity.reset方法，调用下面的方法也是可以的
    //tvTabTitle.setText(tabEntity.showTitle);

    // 高亮则显示showTitle，否则显示默认title
    tvTabTitle.setText(tabEntity.isHighLight ? tabEntity.showTitle : tabEntity.defaultTitle);
  }

  private void addTab(final int position, View tabView) {
    FilterTabEntity tabEntity = mTabEntities.get(position);
    // 赋值position
    tabEntity.position = position;

    final TextView tvTabTitle = (TextView) tabView.findViewById(R.id.tv_tab_title);
    // final ImageView ivTabIcon = (ImageView) tabView.findViewById(R.id.iv_tab_icon);

    // 如果想自定义icon和TextView颜色，可以放到bean中，也可以在View中设置一个方法
    if (mTabTextColor < 0) {
      mTabTextColor = R.color.fitler_title_selector;
    }
    tvTabTitle.setTextColor(getResources().getColorStateList(mTabTextColor));
    tvTabTitle.setSelected(tabEntity.isHighLight);
    tvTabTitle.setText(tabEntity.showTitle);
    // 暂不处理icon的变化，如果需要添加即可，同title处理
    tabView.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        //int position = (int) v.getTag();
        if (mClickTabListener != null) {
          mClickTabListener.onClickTab(mLastPos, position, mTabEntities.get(position));
        }
        mLastPos = position;
      }
    });
    // 设置等分
    addView(tabView, new LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT, 1.0f));
  }

  public interface OnClickTabListener {
    /**
     * @param position position
     * @param entity 当前点击的tab
     */
    void onClickTab(int lastPosition, int position, FilterTabEntity entity);
  }
}
